package com.qu.waibao.network;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.othershe.nicedialog.NiceDialog;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.utils.TDevice;
import com.qu.waibao.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

import info.wangchen.simplehud.SimpleHUD;

/**
 * Created by quguangle on 2018/7/28.
 */
public final class NetManager {
    public static final String BaseUrl = "http://clc.027perfect.com/api/api.mingfa.php?version=v1&vars=";
    private static NetManager mInstance;

    private NetManager() {
    }
    public static synchronized NetManager getInstance() {
        if (mInstance == null)
            mInstance = new NetManager();

        return mInstance;
    }
    /**
     * 获取图片全路径
     * @param path
     * @return
     */
    public static String getFullUrl(String path) {
        String fullUrl = "";
        if (!TextUtils.isEmpty(path)) {
            if (path.startsWith("http")) {
                fullUrl = path;
            } else {
                fullUrl = "http://clc.027perfect.com/" + path;
            }
        }
        return fullUrl;
    }

    /**
     * 上传图片 get_upfile_image
     */
    public static JSONObject image_save_set() {
        JSONObject reg_json = new JSONObject();
        try {
            reg_json.put("action", "image_save_set");
            return reg_json;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;

    }
    /**
     * 获取接口URL
     * @param map
     * @return
     */
    public static RequestParams getUrl(Map<String, String> map) {
        RequestParams params = new RequestParams(BaseUrl);
        if(AppConfig.isEncrypt){
            String request ="vars" + "=" + HttpParamsUtil.getVars(map);
            //params.setBodyContent(DESEncrypt.encrypt(request.toString()));
        } else {
            params.addBodyParameter("vars", HttpParamsUtil.getVars(map));
        }
        return params;
    }
    /**
     * 通用接口请求
     * @param context
     * @param map 参数
     * @param isShowProgress 是否显示进度弹框
     * @param isShowResultMsg  是否显示接口返回的信息
     * @param progressMsg 进度弹框显示的信息
     * @param call 回调
     */
    public void request(final FragmentActivity context, Map<String, String> map, final boolean isShowProgress,
                        final boolean isShowResultMsg, String progressMsg, final XnetCallBack call){
        if (TDevice.hasInternet()) {
            if(isShowProgress) SimpleHUD.showLoadingMessage(context, progressMsg, AppConfig.isCanCancel);
            RequestParams params = getUrl(map);
            x.http().post(params, new Callback.CommonCallback<String>() {

                @Override
                public void onSuccess(String result) {
                    try {
                        JSONObject object = new JSONObject(result);
                        final String msg = object.optString("message");
                        int status = object.optInt("status");
                        switch (status){
                            case 1:
                                call.onSuccess(object.optString("result"),msg);
                                break;

                            case 2:
                                //需要重新登陆


                                break;

                            case 3:
                                //未提交认证
                                if(isShowResultMsg && !TextUtils.isEmpty(msg)) ToastUtil.show(context,msg);

                                break;

                            case 4:
                                //认证正在审核中
                                if(isShowResultMsg && !TextUtils.isEmpty(msg)) ToastUtil.show(context,msg);

                                break;

                            default:
                                if(isShowResultMsg && !TextUtils.isEmpty(msg)) ToastUtil.show(context,msg);
                                break;
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    if(isShowProgress) SimpleHUD.dismiss();
                    call.onError(ex,isOnCallback);
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    if(isShowProgress) SimpleHUD.dismiss();
                }

                @Override
                public void onFinished() {
                    if(isShowProgress) SimpleHUD.dismiss();
                    call.onFinished();
                }
            });
        }
    }


    /**
     * 通用接口请求
     * @param context
     * @param map
     * @param isShowProgress
     * @param call
     */
    public void request(FragmentActivity context, Map<String, String> map, final boolean isShowProgress,
                        final XnetCallBack call){
        request(context,map,isShowProgress,true,"加载中...",call);
    }
}
